package cn.luoshaocheng.user.controller;

import cn.luoshaocheng.basic.exception.BusinessException;
import cn.luoshaocheng.basic.util.PageList;
import cn.luoshaocheng.basic.util.jsonResult;
import cn.luoshaocheng.org.domain.Employee;
import cn.luoshaocheng.org.query.EmployeeQuery;

import cn.luoshaocheng.user.domain.Logininfo;
import cn.luoshaocheng.user.dto.LogininfoDto;
import cn.luoshaocheng.user.dto.WxDto;
import cn.luoshaocheng.user.query.LogininfoQuery;
import cn.luoshaocheng.user.service.ILoginService;
import cn.luoshaocheng.user.service.ILogininfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController //controller层  返回的是json格式数据
@RequestMapping("/login")
public class LogininfoController {

    @Autowired
    private ILogininfoService loginingoService;
    @Autowired
    private ILoginService iLoginService;


    /**
     * 查询全部
     *
     * @return
     */
    @GetMapping
    public List<Logininfo> loadAll() {
        return loginingoService.loadAll();
    }

    /**
     * 查询一个
     *
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Logininfo loadById(@PathVariable("id") Long id) {
        return loginingoService.loadById(id);
    }

    /**
     * 删除
     *
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    public jsonResult deleteById(@PathVariable("id") Long id) {
        try {
            loginingoService.delete(id);
            return new jsonResult();
        } catch (Exception e) {
            e.printStackTrace();
            return jsonResult.me().setMsg("删除失败");
        }
    }

    /**
     * @param
     * @return
     */
    @PutMapping
    public jsonResult addAndUpdate(@RequestBody Logininfo logininfo) {
        try {
            if (logininfo.getId() == null) {
                loginingoService.save(logininfo);
            } else {
                loginingoService.update(logininfo);
            }
            return new jsonResult();
        } catch (Exception e) {
            e.printStackTrace();
            return jsonResult.me().setMsg("操作失败");
        }
    }

    /**
     * 分页数据
     *
     * @param
     * @return
     */
    @PostMapping
    public PageList<Logininfo> queryPage(@RequestBody LogininfoQuery logininfoQuery) {
        return loginingoService.queryPage(logininfoQuery);

    }

    /**
     * 批量删除
     * @param
     * @return
     */
    @PatchMapping
    public jsonResult patchDel(@RequestBody List<Long> ids) {
        try {
            loginingoService.patchDel(ids);
            return new jsonResult();
        } catch (Exception e) {
            e.printStackTrace();
            return jsonResult.me().setMsg("删除失败");
        }
    }
    @PostMapping("/account")
    public jsonResult account(@RequestBody LogininfoDto dto){
        try {
            Map map=iLoginService.account(dto);
            return jsonResult.me().setResultObj(map);
        } catch (BusinessException e) {
            e.printStackTrace();
            return jsonResult.me().setMsg(e.getMessage());
        }catch (Exception e){
            e.printStackTrace();
            return jsonResult.me().setMsg("网络异常！");
        }

    }
    @GetMapping("/weChat/{code}")
    public jsonResult wxChat(@PathVariable("code") String code){
        try {
           return iLoginService.wxChat(code);

        } catch (BusinessException e) {
            e.printStackTrace();
            return jsonResult.me().setMsg(e.getMessage());
        }catch (Exception e){
            e.printStackTrace();
            return jsonResult.me().setMsg("网络异常！");
        }

    }@PostMapping("/wechat/binder")
    public jsonResult binder(@RequestBody WxDto dto){
        try {
            return iLoginService.binder(dto);

        } catch (BusinessException e) {
            e.printStackTrace();
            return jsonResult.me().setMsg(e.getMessage());
        }catch (Exception e){
            e.printStackTrace();
            return jsonResult.me().setMsg("网络异常！");
        }

    }
}

